package com.jrzh.mvc.convert.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.stereotype.Component;

import com.jrzh.common.exception.ProjectException;
import com.jrzh.common.tools.CollectionTools;
import com.jrzh.common.utils.ReflectUtils;
import com.jrzh.framework.I18nHelper;
import com.jrzh.framework.base.convert.BaseConvertI;
import com.jrzh.framework.bean.EasyuiTree;
import com.jrzh.framework.bean.JSTree;
import com.jrzh.framework.bean.SessionUser;
import com.jrzh.framework.bean.TreeViewBean;
import com.jrzh.mvc.constants.SysConstant.RESOURCE_TYPE;
import com.jrzh.mvc.model.sys.ResourcesModel;
import com.jrzh.mvc.view.sys.ResourcesView;


@Component("resourcesConvert")
public class ResourcesConvert implements BaseConvertI<ResourcesModel, ResourcesView>{

	public ResourcesModel addConvert(ResourcesView view) throws ProjectException {
		ResourcesModel model = new ResourcesModel();
		ReflectUtils.copySameFieldToTarget(view, model);
		return model;
	}

	public ResourcesModel editConvert(ResourcesView view, ResourcesModel model) throws ProjectException{
		model.setCode(view.getCode());
		model.setIcon(view.getIcon());
		model.setUrl(view.getUrl());
		model.setName(view.getName());
		model.setPid(view.getPid());
		model.setIsDisable(view.getIsDisable());
		model.setRemark(view.getRemark());
		model.setSortNum(view.getSortNum());
		return model;
	}

	public ResourcesView convertToView(ResourcesModel model) throws ProjectException {
		ResourcesView view = new ResourcesView();
		ReflectUtils.copySameFieldToTarget(model, view);
		view.setTypeName(RESOURCE_TYPE.valueMap.get(view.getType()));
		view.setIconCls(model.getIcon());
		return view;
	}
	
	public EasyuiTree converToEasyuiTree(ResourcesModel model){
		EasyuiTree tree = new EasyuiTree();
		tree.setId(model.getId());
		tree.setPid(model.getPid());
		tree.setIconCls(model.getIcon());
		tree.setText(model.getName());
		Map<String, Object> attributes = new HashMap<String, Object>();
		attributes.put("url", model.getUrl());
		tree.setAttributes(attributes);
		return tree;
	}
	
	public JSTree converToJSTree(ResourcesModel model){
		JSTree tree = new JSTree();
		tree.setId(model.getId());
		tree.setParent(model.getPid());
		tree.setIconCls(model.getIcon());
		tree.setText(model.getName());
		Map<String, Object> attributes = new HashMap<String, Object>();
		attributes.put("url", model.getUrl());
		tree.setAttributes(attributes);
		return tree;
	}
	
	public TreeViewBean convertToTreeView(ResourcesModel model){
		TreeViewBean treeview = new TreeViewBean();
		treeview.setText(model.getName());
		treeview.setHref("#"+model.getCode());
		treeview.setTags(new String[]{model.getId(),model.getPid()});
		Map<String, Boolean> state = new HashMap<String, Boolean>();
		state.put("expanded", model.getExpanded());
		treeview.setState(state);
		treeview.setSortNum(model.getSortNum());
		return treeview;
	}
	
	public ResourcesView convertToViewI18n(ResourcesModel model, SessionUser sessionUser) throws ProjectException{
		ResourcesView resourcesView = convertToView(model);
		try {
			
			resourcesView.setName(I18nHelper.getI18nByKey(model.getName(), sessionUser));
			
		} catch (Exception e) {
			
			resourcesView.setName(model.getName());
		}

		return resourcesView;
	}
	
	public List<ResourcesView> convertToViewListI18n(List<ResourcesModel> modelList, SessionUser sessionUser) throws ProjectException {
		List<ResourcesView> treeList = new ArrayList<ResourcesView>();
		if(CollectionTools.isNotBlank(modelList)){
			for(ResourcesModel root : modelList) {
				//如果中文的话就跳过国际化
				if(isChinese(root.getName())){
					
					treeList.add(convertToView(root));
					
				}else {
					
					treeList.add(convertToViewI18n(root, sessionUser));
				
				}
			}
		}
		return treeList;
	}
	
	public static boolean isChinese(String str) {
		String regEx = "[\u4e00-\u9fa5]";
		Pattern pat = Pattern.compile(regEx);
		Matcher matcher = pat.matcher(str);
		boolean flg = false;
		if (matcher.find())
			flg = true;

		return flg;
	}
}
